package com.codefactory.test;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

public class FreeMarkerExample {
    public static void main(String[] args) throws IOException, TemplateException {
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);

        // 直接使用字符串作为模板
        String templateString = "Hello, ${name}! Welcome to FreeMarker.";

        // 创建模板对象
        Template template = new Template("template", templateString, cfg);

        // 创建数据模型
        Map<String, Object> data = new HashMap<>();
        data.put("name", "Alice");

        // 处理模板
        StringWriter writer = new StringWriter();
        template.process(data, writer);

        // 输出处理后的结果
        System.out.println(writer.toString());
    }
}
